package com.academic_system.controller;

import com.academic_system.model.Project;
import com.academic_system.model.StuProject;
import com.academic_system.model.Student;
import com.academic_system.service.StudentService;
import com.alibaba.fastjson.JSONArray;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.context.request.WebRequest;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/xiangmu")
public class ProjectController {

    @Autowired
    private StudentService studentService;

	@RequestMapping("/shenQingLiXiang")
	public void applyProject(Project project, HttpServletResponse response, HttpSession session) throws IOException {
		PrintWriter out = response.getWriter();
		
		Student student =(Student)session.getAttribute("currStu");

		System.out.println(project.toString());

		boolean flag = studentService.addProjet(project);
		
		if(flag) {
			out.write("false");
		}else {
			out.write("true");
		}
	}
	
	@RequestMapping("/findAllProject")
	@ResponseBody
	public Object findAllProject() {
		
		return null;
	}
	@RequestMapping("/findAllMyProject")
	@ResponseBody
	public Object findAllMyProject() {
		List<StuProject>projects = studentService.findAllStuProject();
		for (StuProject project : projects) {
			System.out.println(project);
		}
		return JSONArray.toJSONString(projects);
	}

//	@InitBinder
//	protected void init(HttpServletRequest request, ServletRequestDataBinder binder) {
//		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
//		dateFormat.setLenient(false);
//		binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, false));
//	}


	@InitBinder
	public void initBinder(WebDataBinder binder, WebRequest request) {
		//转换日期
		DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
		binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));// CustomDateEditor为自定义日期编辑器
	}
}
